//
//  PhotoBrowserAnimator.swift
//  JJWeibo
//
//  Created by JJ on 15/9/16.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit
import SDWebImage

class PhotoBrowserAnimator: NSObject,UIViewControllerTransitioningDelegate {
    
    var fromRect = CGRectZero
    var toRect = CGRectZero
    var url:NSURL?
    
    weak var picView:StatusPictureView?

    var isPresent:Bool = false
    
    private lazy var imageView:JJProgressImageView = {
        
        let imageview=JJProgressImageView()
        imageview.contentMode=UIViewContentMode.ScaleAspectFill
        imageview.clipsToBounds=true
        return imageview
        
    }()
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent=true
        
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent=false
        
        return self
    }
    
    func prepareAnimator(picView:StatusPictureView,fromRect:CGRect,toRect:CGRect,url:NSURL){
        
        self.picView=picView
        self.fromRect=fromRect
        self.toRect=toRect
        self.url=url
    }
    
}

extension PhotoBrowserAnimator:UIViewControllerAnimatedTransitioning{
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 1.0
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        let fromView=transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        
        let toView=transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        
        printLog(fromView)
        printLog(toView)
        
        isPresent ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)
        
//        presentAnimation(transitionContext)
        
    }
    
    private func presentAnimation(transitionContext: UIViewControllerContextTransitioning){
        
        
        let contenerView = transitionContext.containerView()
        
//        let fromView=transitionContext.viewForKey(UITransitionContextFromViewKey)!
//
//        contenerView?.addSubview(fromView)
        
        contenerView?.addSubview(imageView)
        imageView.frame = fromRect
        
       imageView.sd_setImageWithURL(url, placeholderImage: nil, options: SDWebImageOptions.RetryFailed, progress: { (current, total) -> Void in
    
        let progress = CGFloat(current / total)
    
        self.imageView.progress = progress
        
        }) { (_, error, _, _) -> Void in
            
            if  error != nil{
                
                transitionContext.completeTransition(false)
                true
            }
        }
     
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            self.imageView.frame = self.toRect
            
            }, completion: { (_) -> Void in
                
                self.imageView.removeFromSuperview()
                let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
                let contenerView = transitionContext.containerView()
                contenerView?.addSubview(toView)
                transitionContext.completeTransition(true)
        })
                
    }
    
    private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning){
        
        let fromVC=transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as!PhotoBrowserViewController
        let currentView=fromVC.currentView
        let imageIndex=fromVC.currentImageIndex
        
        let fromView=transitionContext.viewForKey(UITransitionContextFromViewKey)!
        fromView.removeFromSuperview()
        
        let contener=transitionContext.containerView()
        contener?.addSubview(currentView)
         currentView.center=fromView.center
        let scale=fromVC.view.transform.a
        currentView.transform=CGAffineTransformScale(currentView.transform, scale, scale)
        currentView.alpha=scale

        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            currentView.frame = self.picView!.screenRect(imageIndex)
            currentView.alpha=1.0
            }, completion: { (_) -> Void in
 
                currentView.removeFromSuperview()
                transitionContext.completeTransition(true)
        })
        
    }
    
    
    
    
}









